package info.peigen.hotpot.manage.core.handler;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Component;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.Filter;
import org.noear.solon.core.handle.FilterChain;
import org.smartboot.http.server.HttpRequest;

/**
 * <b>(GlobalTokenFilter)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2024/7/24
 */
@Slf4j
@Component
public class GlobalTokenFilter implements Filter {
    @Override
    public void doFilter(Context ctx, FilterChain chain) throws Throwable {
        HttpRequest request = (HttpRequest) ctx.request();
        String      token   = request.getParameter("token");
        if (StrUtil.isNotEmpty(token)) {
            Assert.notNull(StpUtil.getLoginIdByToken(token), () -> new BusinessException(ResultCode.USER_NOT_LOGIN));
        }
        chain.doFilter(ctx);
    }
}
